package com.example.springamqpstudy.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcase.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void ListenTopicQueue1(String message) {
        System.out.println("queue1 接收到的 " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcase.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void ListenTopicQueue2(String message) {
        System.out.println("queue2 接收到的 " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),  // 队列名称
            exchange = @Exchange(name = "itcase.direct", type = ExchangeTypes.DIRECT), // 交换机的名称以及类型
            key = {"red", "blue"}
    ))
    public void listenDirectQueue1(String message) {
        System.out.println("queue1 接收到的 " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),  // 队列名称
            exchange = @Exchange(name = "itcase.direct", type = ExchangeTypes.DIRECT), // 交换机的名称以及类型
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue2(String message) {
        System.out.println("queue2 接收到的 " + message);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenerWorkQueue1(String message) throws InterruptedException {
        System.out.println("work1接收到的消息 : " + message);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenerWorkQueue2(String message) throws InterruptedException {
        System.err.println("work2接收到的消息 : " + message);
        Thread.sleep(200);
    }


    @RabbitListener(queues = "simple.queue")
    public void listenerSimpleQueue(String message) {
        System.out.println("接收到的消息 : " + message);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenerFanoutQueue1(String message) {
        System.out.println("fanout queue1接收到的消息 : " + message);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenerFanoutQueue2(String message) {
        System.out.println("fanout queue2接收到的消息 : " + message);
    }
}
